package com.culturalCenter.dataCenter.service.impl.user;

import com.culturalCenter.dataCenter.Utils.DateFormatUtil;
import com.culturalCenter.dataCenter.Utils.MathUtil;
import com.culturalCenter.dataCenter.customEntity.responseParam.user.UserPointListResponse;
import com.culturalCenter.dataCenter.customEntity.user.CustomPointStatistics;
import com.culturalCenter.dataCenter.customEntity.user.CustomUserPointChangeCount;
import com.culturalCenter.dataCenter.entity.user.UserPoint;
import com.culturalCenter.dataCenter.mapper.user.UserDayPointMapper;
import com.culturalCenter.dataCenter.mapper.user.UserPointDecreaseMapper;
import com.culturalCenter.dataCenter.mapper.user.UserPointIncreaseMapper;
import com.culturalCenter.dataCenter.mapper.user.UserPointMapper;
import com.culturalCenter.dataCenter.service.user.UserPointService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * UserPoint业务层接口实现类
 **/
@Service
public class UserPointServiceImpl implements UserPointService {

    @Resource
    private UserPointMapper userPointMapper;
    @Resource
    private UserPointIncreaseMapper userPointIncreaseMapper;
    @Resource
    private UserPointDecreaseMapper userPointDecreaseMapper;
    @Resource
    private UserDayPointMapper userDayPointMapper;

    @Override
    public PageInfo<UserPoint> findPage(int page, int size) {
        return PageHelper.startPage(page, size).doSelectPageInfo(() -> userPointMapper.findPageAll());
    }

    @Override
    public Integer delete(String id) {
        return userPointMapper.deleteById(id);
    }

    @Override
    public Integer update(UserPoint userPoint) {
        return userPointMapper.update(userPoint);
    }

    @Override
    public Integer add(UserPoint userPoint) {
        return userPointMapper.insert(userPoint);
    }

    @Override
    public UserPoint findById(String id) {
        return userPointMapper.queryById(id);
    }

    @Override
    public List<UserPoint> findAll(UserPoint userPoint) {
        return (List<UserPoint>) userPointMapper.queryAll(userPoint);
    }

    @Override
    public PageInfo<UserPointListResponse> findPointList(String userId, int pageNum, int pageSize) {
        List<UserPointListResponse> userPointListResponseList = new ArrayList<>();
        List<String> dateList = userPointMapper.findDateList((pageNum - 1) * pageSize, pageSize);
        if (dateList.size() > 0) {
            for (String date : dateList) {
                List<UserPoint> userPointList = userPointMapper.findListByUserAndDate(userId, date);
                userPointListResponseList.add(new UserPointListResponse(date, userPointList));
            }
        }
        long total = userPointMapper.finDateCount();
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<UserPointListResponse> pageInfo = new PageInfo<>(userPointListResponseList);
        PageHelper.clearPage();
        pageInfo.setTotal(total);
        return pageInfo;
    }

    @Override
    public CustomPointStatistics findPointStatisticsByUserId(String userId) {
        CustomPointStatistics customPointStatistics = new CustomPointStatistics();
        customPointStatistics.setCurrentPoint(userDayPointMapper.queryByUserIdAndDate(userId, DateFormatUtil.format(new Date())));
        customPointStatistics.setDayPointList(userDayPointMapper.queryByUserIdAndMonth(userId, DateFormatUtil.formatMonth(new Date())));
        List<CustomUserPointChangeCount> userPointDecreaseList = MathUtil.calculateRate(userPointDecreaseMapper.queryRateByUserId(userId));
        List<CustomUserPointChangeCount> userPointIncreaseList = MathUtil.calculateRate(userPointIncreaseMapper.queryRateByUserId(userId));
        customPointStatistics.setPointDecreaseList(userPointDecreaseList);
        customPointStatistics.setPointIncreaseList(userPointIncreaseList);
        return customPointStatistics;
    }

}
